package app.fugema.shop.main.order.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DateUtils;
import com.base.common.utils.JavaMethod;
import com.base.common.utils.LogUtil;
import com.base.common.utils.RxTimer;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityGroupOrderDetailBinding;
import app.fugema.shop.databinding.GroupBookingDetailHeadImageItemBinding;
import app.fugema.shop.main.order.adapter.GroupOrderDetailAdapter;
import app.fugema.shop.main.order.model.GroupDetailResult;
import app.fugema.shop.main.order.model.GroupPeopleHeadBean;
import app.fugema.shop.main.order.model.OrderAddressHeadBean;
import app.fugema.shop.main.order.model.OrderDetailResult;
import app.fugema.shop.main.order.model.OrderPayFootBean;
import app.fugema.shop.main.order.model.TheOrder;
import app.fugema.shop.main.order.vm.GroupBookingVM;
import app.fugema.shop.main.order.vm.OrderListVM;

import java.util.ArrayList;
import java.util.List;

public class GroupBookingDetailActivity extends BaseTitleActivity<ActivityGroupOrderDetailBinding, GroupBookingVM> {

    public static void start(Context context, long groupOnId)
    {
        Intent starter = new Intent(context, GroupBookingDetailActivity.class);
        starter.putExtra("groupOnId", groupOnId);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_order_detail);

    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    private RxTimer rxTimer;

    private SingleRecyclerAdapter<String, GroupBookingDetailHeadImageItemBinding> adapter;

    private String userkey;

    private GroupOrderDetailAdapter orderDetailAdapter;

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("拼团详情",R.color.white);
        adapter = new SingleRecyclerAdapter<String, GroupBookingDetailHeadImageItemBinding>() {
            @Override
            public int getLayoutId()
            {
                return R.layout.group_booking_detail_head_image_item;
            }

            @Override
            public void onBindViewHolder(GroupBookingDetailHeadImageItemBinding binding, int position, String bean)
            {
                super.onBindViewHolder(binding, position, bean);
                if (position == 0) {
                    binding.tvTitle.setVisibility(View.VISIBLE);
                } else {
                    binding.tvTitle.setVisibility(View.GONE);
                }
                if (UIUtils.isEmpty(bean)) {
                    binding.givHeadImage.loadUrl(R.mipmap.ic_booking_heard);
                } else {
                    binding.givHeadImage.loadUrl(bean);
                }
            }
        };
        binding.rvRecycler.setAdapter(adapter);

        orderDetailAdapter = new GroupOrderDetailAdapter(GroupBookingDetailActivity.this, new OrderListVM(getApplication()));
        binding.rvRecyclerView.setAdapter(orderDetailAdapter);
    }


    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        userkey = LoginUtils.getToken();

        long groupOnId = intent.getLongExtra("groupOnId", 0);
        viewModel.groupOrderInfo(groupOnId , userkey).observe(this, new BaseViewObserver<LiveDataWrapper<GroupDetailResult>>()
        {
            @Override
            protected void onSuccess(LiveDataWrapper<GroupDetailResult> data)
            {
                GroupDetailResult.OrderDetailBean bean = data.data.getOrderDetail();
                binding.setItem(bean);
                if (bean.getJoinStatus() == 0)
                {
                    binding.tvStatusText.setText("拼团中");
                    binding.llSucc.setVisibility(View.GONE);
                    binding.llBooking.setVisibility(View.VISIBLE);
                    binding.llBookingTime.setVisibility(View.VISIBLE);
                    if (rxTimer == null) {
                        rxTimer = new RxTimer();
                    }
                    else
                    {
                        rxTimer.cancel();
                    }
                    rxTimer.interval(1000, new RxTimer.RxAction()
                    {
                        @Override
                        public void action(long number)
                        {
                            getDateTime(bean.getJoinTime(), bean.getSeconds());
                        }
                    });
                }
                else if (bean.getJoinStatus() == 1)
                {
                    binding.tvStatusText.setText("拼团成功");
                    binding.llSucc.setVisibility(View.VISIBLE);
                    binding.llBooking.setVisibility(View.GONE);
                    binding.llBookingTime.setVisibility(View.GONE);
                }
                else if (bean.getJoinStatus() == 2)
                {
                    binding.tvStatusText.setText("拼团失败");
                    binding.tvHelp.setText("拼团时间已到");

                    binding.llSucc.setVisibility(View.GONE);
                    binding.llBooking.setVisibility(View.VISIBLE);
                    binding.llBookingTime.setVisibility(View.VISIBLE);
                }



                List<String> list = new ArrayList<>();
                if (UIUtils.isNotEmpty(bean.getUserInfo()))
                {
                    for (int i = 0; i < bean.getUserInfo().size(); i++)
                    {
                        list.add(bean.getUserInfo().get(i).getPhoto());
                    }
                }
                adapter.setDataList(list);
                //设置显示 拼团人数
                GroupPeopleHeadBean groupPeople = GroupPeopleHeadBean.getGroupPeopleByGroupInfo( bean );
                orderDetailAdapter.addHeaderView(groupPeople);

                //查询到 拼团信息 后在
                viewModel.getOrderDetail( bean.getOrderId() ,userkey )
                         .observe(GroupBookingDetailActivity.this, new BaseViewObserver<LiveDataWrapper<OrderDetailResult>>()
                         {
                             @Override
                             protected void onSuccess(LiveDataWrapper<OrderDetailResult> data)
                             {
                                 showOrderDetail( data.data );
                             }
                         });


            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<GroupDetailResult> data)
            {
                return UIUtils.isEmpty(data.data.getOrderDetail());
            }
        });
    }


    //显示页面
    private void showOrderDetail(OrderDetailResult orderResult)
    {
        TheOrder orderInfo = orderResult.getOrder();

        //PublicGlobal.getOrderState(orderInfo.getOrderStatus()) 订单状态转文字
        binding.tvOrderStatus.setText( orderInfo.getStatus() );
        // 设置订单状态提示语（没有）
        //binding.tvStatusTips.setVisibility(View.VISIBLE);
        //binding.tvStatusTips.setText("商家正在备货中，请耐心等候");

        switch (orderInfo.getOrderStatus())
        {
            case 1:
            case 2:
            case 3:
            case 4:
                binding.ivStatusImg.setImageResource(R.mipmap.ic_order_waiting);
                break;
            case 5:
                binding.ivStatusImg.setImageResource(R.mipmap.ic_order_complate);
                break;
            case 6:
                binding.ivStatusImg.setImageResource(R.mipmap.ic_order_refuse);
                break;
        }

        LogUtil.e("设置页面信息");
        //显示地址
        OrderAddressHeadBean headBean = OrderAddressHeadBean.getAddressByOrderInfo(orderInfo);
        headBean.setState(1);
        orderDetailAdapter.addHeaderView(headBean, 1);

        orderDetailAdapter.setDataList(orderResult.getOrderItem());

        OrderPayFootBean footerBean = OrderPayFootBean.getOrderFootByOrderInfo(orderInfo);
        footerBean.setItemType(BaseItemTypeInterface.TYPE_FOOT);
        orderDetailAdapter.addFooterView(footerBean);
        /*
        OrderDetailFooterBean orderDetailFooterBean = new OrderDetailFooterBean();
        orderDetailFooterBean.setItemType(BaseItemTypeInterface.TYPE_FOOT_ONE);
        orderDetailFooterBean.setOrderNumber(orderNumber);
        orderDetailAdapter.addFooterView(orderDetailFooterBean);
        */

        //待付款1  和 待收货  显示倒计时    OrderStatus 1:待付款 2:待发货 3:待收货 4:待评价 5:成功 6:失败
        if (orderResult.getOrder().getOrderStatus() == 1 || orderResult.getOrder().getOrderStatus() == 3)
        {

        }
        //待发货
        else if (orderResult.getOrder().getOrderStatus() == 2)
        {
            binding.tvDateTime.setVisibility(View.VISIBLE);
            binding.tvDateTime.setText("等待发货中，请您耐心等待");
        }
    }


    public void getDateTime(String startTime, int limitTime)
    {
        long time = DateUtils.getHour(DateUtils.dateStringToLong(startTime), limitTime);
        int spec = (int) (time - System.currentTimeMillis()) / 1000;

        if (spec <= 0) {
//            return "";
            binding.rtvTimeHour.setText("00");
            binding.rtvTimeMinute.setText("00");
            binding.rtvTimeSecond.setText("00");
        } else {

            int h = (int) (spec / 3600);
            int m = (int) (spec % 3600) / 60;
            int s = (int) (spec % 60);

            binding.rtvTimeHour.setText(JavaMethod.intValueOfString(h, 2));
            binding.rtvTimeMinute.setText(JavaMethod.intValueOfString(m, 2));
            binding.rtvTimeSecond.setText(JavaMethod.intValueOfString(s, 2));
//            return h + "时" + m + "分" + s + "秒";
        }
    }


    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (rxTimer != null)
        {
            rxTimer.cancel();
        }
    }
}
